/**
 * Created by wrl on 2017/7/10.
 */
$(function(){
    $(document).keyup(function (e) {
        if(e.which==13){
            $('#selectBtnEnter').click();
        }
    })
});
var path = '../';
var pagePath = path+'role/selectPaging';
var updateId = '';
Vue.http.options.emulateJSON = true;
$(function(){
    var vue = new Vue({
        el:'#role-list',
        data:{
            ids:[],
            roleInfo:{},
            searchData:{
                page:1,
                rows:5,
                roleName:null,
                isSearch:'0'
            },
            permissions:{},//权限
            showPage:5      /*显示页码个数*/
        },
        mounted:function(){
            var self = this;
            self.$http.get(pagePath,{}).then(function(response){
                self.roleInfo = response.body.pagination;
                console.log(self.roleInfo)
                customUtils.paging('#pagination',self.roleInfo.pages,self.showPage,function(num) {
                    currentPage = num;
                    if(num == self.searchData.page)
                        return;
                    self.searchData.page = num;
                    self.$http.post(pagePath,self.searchData).then(function(response){
                        self.roleInfo = response.body.pagination;
                    })
                })
            },function(response){
                console.log('请求失败');
            });
            //遍历权限
	    	  $.each(parent.globalPermissions, function(i,val) {
	    		  self.$data.permissions[val.replace(':','_')]=true;
	    	  });
        },
        filters: {
        },
        methods: {
            cbBtn:function(e){
                this.ids.push($(e.target).val())
            },
            search: function () {       //查
                var self = this;
                self.searchData.page = 1;
                self.searchData.isSearch = '1';
                self.$http.post(pagePath,self.searchData).then(function(response){
                    self.roleInfo = response.body.pagination;
                    if(self.roleInfo.total == 0) return
                    customUtils.paging('#pagination',self.roleInfo.pages,self.showPage,function(num) {
                        currentPage = num;
                        if(num == self.searchData.page)
                            return;
                        self.searchData.page = num;
                        self.$http.post(pagePath,self.searchData).then(function(response){
                            self.roleInfo = response.body.pagination;
                        })
                    })
                },function(response){
                    console.log(response.body)
                })
            },
            updatePage:function(id){     //改
                layer_show('修改角色', 'admin-role-add-update.html?action=update&updateId='+id, '900');
            },
            delIDs:function(){
                var self = this;
                if(self.ids.length != 0){
                    layer.confirm('确认删除？',function(index){
                        self.$http.get(path+'role/deleteByIds?ids='+self.ids,{}).then(function(response){
                            if(response.body.flag){
                                tips('操作成功',6)
                                setTimeout(function(){
                                    location.replace(location.href);
                                },500)
                            }else{
                                tips('删除失败',5)
                            }
                        },function(response){
                            console.log(response.body)
                            tips('操作失败',5)
                        })
                    })
                }else{
                    tips('请选择需要删除的功能',5)
                }
            },
            del:function(id){        //删
                var self = this;
                layer.confirm('功能删除须谨慎，确认要删除吗？', function(index) {
                    var url = path+'role/deleteById';
                    self.$http.post(url,{'id':id}).then(function(response){
                        if(response.body.flag == true){
                            tips('已删除!',1)
                            setTimeout(function(){
                                location.replace(location.href);
                            },500);
                        }else{
                            tips('删除失败!',5)
                        }
                    },function(res){
                        console.log(res.body)
                    })
                })
            },
            addData:function(){         //增
                layer_show('添加角色', 'admin-role-add-update.html', '900');
            }
        }
    });
})